package com.example.famhealing.timeline;

import java.util.ArrayList;
import java.util.Calendar;

import com.example.famhealing.R;
import com.example.famhealing.schedule.CalendarMonthAdapter;
import com.example.famhealing.schedule.CalendarPopup;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;

public class ConditionFragment extends Fragment {

	EditText dayText;
	
	ImageButton setDate;
	
	int curYear;
	int curMonth;
	int curDay;
	
	int year;
	int month;
	int day;
	
	String sDay;
	
	public static String searchDate = "";
	
	private ArrayList<Condition_List_Data> Array_Data;
    private Condition_List_Data data;
    private ConditionListAdapter adapter;
 
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View v = inflater.inflate(R.layout.fragment_condition, container, false);
		
        dayText = (EditText) v.findViewById(R.id.dayText);
        
        Calendar cal= Calendar.getInstance ();
        
        cal.set(Calendar.YEAR, TimelineActivity.selectedYear);
        cal.set(Calendar.MONTH, TimelineActivity.selectedMonth-1);
        cal.set(Calendar.DATE, TimelineActivity.selectedDay);
        
        sDay = getDay(cal);
        
        setDayText(TimelineActivity.selectedYear, TimelineActivity.selectedMonth, TimelineActivity.selectedDay, sDay);
        
        setDate = (ImageButton) v.findViewById(R.id.setDate);
        setDate.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				//날짜 하나만 선택 가능하도록
		        CalendarMonthAdapter.multiSelect = false;
		        
		        //타임라인 날짜 검색 위한 팝업창(달력) 띄우기
				Intent intent = new Intent(getActivity(), CalendarPopup.class);
				startActivity(intent);
			}
        	
        });
        
        //임시로 넣어놓은 데이터, 이후에는 파싱한 정보 입력
      	Array_Data = new ArrayList<Condition_List_Data>();
        data = new Condition_List_Data("메모1", "약품1, 약품2", "기분굳", "상태굳");
        Array_Data.add(data);
        data = new Condition_List_Data("메모2", "약품3, 약품4", "쏘쏘해", "별로여");
        Array_Data.add(data);
        data = new Condition_List_Data("메모3", "약품5, 약품6, 약품7", "그닥", "메롱");
        Array_Data.add(data);
             
        ListView condition_list = (ListView) v.findViewById(R.id.condition_List);
        adapter = new ConditionListAdapter(getActivity(), android.R.layout.simple_list_item_1, Array_Data);
        condition_list.setAdapter(adapter);
			
		return v;
	}
	
	/*
	private DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
	       
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                  int dayOfMonth) {
             // TODO Auto-generated method stub
        	
        	TimelineActivity.selectedYear = year;
        	TimelineActivity.selectedMonth = monthOfYear+1;
        	TimelineActivity.selectedDay = dayOfMonth;
        	
        	Calendar cal= Calendar.getInstance ();
            
            cal.set(Calendar.YEAR, year);
            cal.set(Calendar.MONTH, monthOfYear);
            cal.set(Calendar.DATE, dayOfMonth);
            
            sDay = getDay(cal);
        	
        	setDayText(year, monthOfYear+1, dayOfMonth, sDay);
        	
        }
	};
	*/
	
	private String getDay(Calendar calendar) {
		
		switch (calendar.get(Calendar.DAY_OF_WEEK)){
        case 1:
            sDay = "일";
            break;
        case 2:
        	sDay = "월";
            break;
        case 3:
        	sDay = "화";
            break;
        case 4:
        	sDay = "수";
            break;
        case 5:
        	sDay = "목";
            break;
        case 6:
        	sDay = "금";
            break;
        case 7:
        	sDay = "토";
            break;
        }
		return sDay;
	}
	
	private void setDayText(int year, int month, int date, String day) {
		
		if(month < 10) {
    		if(date < 10) {
    			dayText.setText(year + ".0" + month + ".0" + date + "(" + day + ")");
    		} else {
    			dayText.setText(year + ".0" + month + "." + date + "(" + day + ")");
    		}
    	} else {
    		if(date < 10) {
    			dayText.setText(year + "." + month + ".0" + date + "(" + sDay + ")");
    		} else {
    			dayText.setText(year + "." + month + "." + date + "(" + sDay + ")");
    		}
    	}
		
	}
}